package bean;

import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;

import java.io.File;

public class ClassConfigBean {
    // 生成文件所在目标文件夹，父级路径，如果就处于当前选中的文件下，那么不需要传此字段，如果需要同时创建 1,2,3 级目录，则传进来就可以
    private final String[] folder;
    // 模板文件名
    private final String templateFile;
    // 对应生成的文件名，不含后缀
    private final String generateFileName;

    public ClassConfigBean(String templateFile, String generateFileName, String... parentFolders) {
        this.folder = parentFolders == null ? new String[0] : parentFolders;
        this.templateFile = templateFile;
        this.generateFileName = generateFileName;
    }

    public String getTemplateFile() {
        return "templates/" + templateFile + ".txt";
    }

    public File createFile(PsiDirectory root) {
        for (String name : folder) {
            PsiDirectory dir = root.findSubdirectory(name);
            if (dir == null) {
                dir = root.createSubdirectory(name);
            }
            root = dir;
        }
        PsiFile psiFile = root.createFile(generateFileName);
        return new File(psiFile.getVirtualFile().getPath());
    }
}
